﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using FCModel;
using System.Reflection;
using FCBLL;
using MiscLib;

namespace FileConverter.LAB
{
    public partial class HGSAResonance : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string logPath = Server.MapPath("~/Excel/");
            //判断是否有文件
            if (FileUploadTestResult.PostedFile.FileName == "" || FileUploadTestResult.PostedFile.InputStream.Length < 1)
            {
                lblInfo.Text = "TestResult 没有指定文件.";
                return;
            }
            if (FileUploadDataSummary.PostedFile.FileName == "" || FileUploadDataSummary.PostedFile.InputStream.Length < 1)
            {
                lblInfo.Text = "Data Summary 没有指定文件.";
                return;
            }
            try
            {
                //判断文件类型
                string TestResultFile = Guid.NewGuid().ToString() + Path.GetExtension(FileUploadTestResult.FileName);
                string DataSummaryFile = Guid.NewGuid().ToString() + Path.GetExtension(FileUploadDataSummary.FileName);
                if (!TestResultFile.EndsWith(".zip", StringComparison.OrdinalIgnoreCase))
                {
                    lblInfo.Text = "TestResult 文件格式不正确";
                    return;
                }
                if (!DataSummaryFile.EndsWith(".csv", StringComparison.OrdinalIgnoreCase))
                {
                    lblInfo.Text = "Data Summary 文件格式不正确";
                    return;
                }
                if (!Directory.Exists(Server.MapPath("~/HGSAResonance/")))
                {
                    Directory.CreateDirectory(Server.MapPath("~/HGSAResonance/"));
                }
                FileUploadTestResult.PostedFile.SaveAs(Server.MapPath("~/HGSAResonance/") + TestResultFile);
                FileUploadDataSummary.PostedFile.SaveAs(Server.MapPath("~/HGSAResonance/") + DataSummaryFile);
                string zipDirectory = Server.MapPath("~/HGSAResonance/UnZip/" + Guid.NewGuid().ToString() + "/");
                if (!Directory.Exists(zipDirectory))
                {
                    Directory.CreateDirectory(zipDirectory);
                }
                //解压，保存
                MiscLib.SharpZipLib.UnZip(Server.MapPath("~/HGSAResonance/") + TestResultFile, zipDirectory);
                List<string> listSummaryData = File.ReadAllLines(Server.MapPath("~/HGSAResonance/") + DataSummaryFile).ToList();
                ///删除错误数据，已长度不到100判断
                listSummaryData = listSummaryData.AsEnumerable().Where(p => p.Length > 100).ToList<string>();

                List<string> listTestResult = Directory.GetFiles(zipDirectory).ToList();//解压后文件组
                if (listSummaryData.Count - 1 != listTestResult.Count)
                {
                    lblInfo.Text = string.Format(@"TestResult :{0} 与 Data Summary :{1}文件HSA数目不一样", listTestResult.Count, listSummaryData.Count);
                    return;
                }
                //Read Summary Data .csv,产生tdf文件名
                string SummaryDataTDFFileName = "";
                string RawDataTDFFileName = "";
                string zipName = "";
                if (listSummaryData[2].Split(',')[2].ToString().ToLower().Contains("kai"))
                {
                    SummaryDataTDFFileName = HGSAResonanceBLL.getSummaryDataFileName(zipDirectory, "C", "Summary");
                    RawDataTDFFileName = "C";
                    zipName = HGSAResonanceBLL.getZipName("KAIC");
                }
                if (listSummaryData[2].Split(',')[2].ToString().ToLower().Contains("ksu"))
                {
                    SummaryDataTDFFileName = HGSAResonanceBLL.getSummaryDataFileName(zipDirectory, "X", "Summary");
                    RawDataTDFFileName = "X";
                    zipName = HGSAResonanceBLL.getZipName("KSUX");
                }
                List<SummaryData> listHR = HGSAResonanceBLL.ReadSummaryData(listSummaryData);
                //Read TestResult 
                List<TestResult> listTR = HGSAResonanceBLL.ReadTestResult(listTestResult);
                //生成Summary Data TDF
                HGSAResonanceBLL.MakeSummaryDataTDF(SummaryDataTDFFileName, listHR);
                //生成Raw Data TDF
                List<RawData> listRD = HGSAResonanceBLL.getRawDataByFile(listHR, listTR);
                List<string> downLoadList = HGSAResonanceBLL.MakeRawDataTDFByList(zipDirectory, RawDataTDFFileName, listRD);
                //打包文件
                downLoadList.Add(SummaryDataTDFFileName);
                //OCR文件生成
                DateTime dtNow = DateTime.Now;
                string OcrTDFFileName = HGSAResonanceBLL.BuildOcrData(RawDataTDFFileName, Server.MapPath("~/HGSAResonance/"), listHR, dtNow);
                string OcrZipFileName = Server.MapPath("~/HGSAResonance/") + RawDataTDFFileName + dtNow.ToString("ddMMyyyyHH") + HGSAResonanceBLL.readOCRSeq(RawDataTDFFileName, dtNow.ToString("ddMMyyyyHH"), "OCR") + ".zip";
                MiscLib.SharpZipLib.Zip(OcrZipFileName, new List<string>() { OcrTDFFileName });
                downLoadList.Add(OcrZipFileName);
                MiscLib.SharpZipLib.Zip(Server.MapPath("~/HGSAResonance/") + zipName, downLoadList);
                //判断文件大小
                FileInfo file = new FileInfo(Server.MapPath("~/HGSAResonance/") + zipName);
                if (file.Length > 2 * 1024 * 1024)
                {
                    throw new Exception("压缩包大小超过2M");
                }
                //下载文件
                ExcelExport.DownLoadFile(Server.MapPath("~/HGSAResonance/") + zipName, HttpContext.Current.Response, Server, logPath);
                //删除临时文件
                File.Delete(Server.MapPath("~/HGSAResonance/") + TestResultFile);
                File.Delete(Server.MapPath("~/HGSAResonance/") + DataSummaryFile);
                HGSAResonanceBLL.DeleteFolder(zipDirectory);
            }
            catch (Exception err)
            {
                lblInfo.Text = err.Message;
            }
        }
    }
}